﻿using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Assertions;

public class EventTriggerListener : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler, IPointerDownHandler, IPointerEnterHandler,
IPointerExitHandler, IPointerUpHandler, ISelectHandler, IUpdateSelectedHandler, IScrollHandler
{
    public Action<GameObject,PointerEventData> onClick;
    public Action<GameObject,PointerEventData> onDown;
    public Action<GameObject,PointerEventData> onEnter;
    public Action<GameObject,PointerEventData> onExit;
    public Action<GameObject,PointerEventData> onUp;
    public Action<GameObject,PointerEventData> onBeginDrag;
    public Action<GameObject,PointerEventData> onDrag;
    public Action<GameObject,PointerEventData> onEndDrag;
    //BaseEventData
    public Action<GameObject, BaseEventData> onSelect;
    public Action<GameObject, BaseEventData> onUpdateSelect;

    public Action<GameObject,PointerEventData> onScroll;

    public string btnClickSound = "";
    public string tabClickSound = "button_tab";
    //public int UIType = 0;
    //void Start()
    //{
    //    if(this.GetComponent<Toggle>()!=null)
    //        UIType = 2;
    //    Image img = this.GetComponent<Image>();
    //    if (img != null && img.sprite != null)
    //    {
    //        if (img.sprite.name == "common_button_close")
    //        {
    //            UIType = 1;
    //            btnClickSound = "button_cancel";
    //        }
    //        else if (img.sprite.name.IndexOf("common_button") >= 0)
    //        {
    //            UIType = 1;
    //            btnClickSound = "button_ok";
    //        }
    //    }
    //}
    public static void AddToggleListener(Toggle com,UnityEngine.Events.UnityAction<bool> Callback)
    {
        Assert.IsTrue(com != null, "EventTriggerListener.Get(), go == null");
        com.onValueChanged.AddListener(Callback);
    }
    public static EventTriggerListener Get(GameObject go)
    {
        Assert.IsTrue(go != null, "EventTriggerListener.Get(), go == null");

        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if (listener == null) listener = go.AddComponent<EventTriggerListener>();
        return listener;
    }

    public static void Remove(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if (listener != null) Component.Destroy(listener);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null)
            onClick(gameObject, eventData);
    }
    public void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null)
            onDown(gameObject, eventData);
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        if (onEnter != null)
            onEnter(gameObject, eventData);
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        if (onExit != null)
            onExit(gameObject, eventData);
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null)
            onUp(gameObject, eventData);
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        if (onBeginDrag != null)
        {
            onBeginDrag(gameObject, eventData);
        }
        else
        {
            //ExecuteEvents.GetEventHandler<IDragHandler>(this.transform.parent.gameObject);
            
        }
        //让scrollrect获取拖放事件
        UnityEngine.UI.ScrollRect sc = this.GetComponentInParent<UnityEngine.UI.ScrollRect>();
        if (sc != null)
        {
            eventData.pointerDrag = sc.gameObject;
            sc.OnBeginDrag(eventData);
        }
    }
    public void OnDrag(PointerEventData eventData)
    {
        if (onDrag != null)
            onDrag(gameObject, eventData);
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        if (onEndDrag != null)
            onEndDrag(gameObject, eventData);
        UnityEngine.UI.ScrollRect sc = this.GetComponentInParent<UnityEngine.UI.ScrollRect>();
        if (sc != null)
        {
            eventData.pointerDrag = sc.gameObject;
            sc.OnEndDrag(eventData);
        }
    }
    public void OnScroll(PointerEventData eventData)
    {
        if (onScroll != null)
            onScroll(gameObject, eventData);
    }

    public void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null)
            onSelect(gameObject, eventData);
    }
    public void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null)
            onUpdateSelect(gameObject, eventData);
    }

    void OnDestroy()
    {
        Remove(gameObject);
    }
}